use crate::components::{BoxColour,Position,Difficulty,Gameplay};
use crate::entities::*;
use ggez::audio::Source;
use ggez::Context;
use hecs::World;

pub fn level1_map() -> &'static str {
    "
    W W W W W W W W W W W W W W W W W W W W
    W F F F F F F F F F F F F F F F F F F W
    W F F F F F F F F F F F F F F F F F F W
    W F F F F F F F F F F F F F F F F F F W
    W F F F B F F F F F F F F F F F F F F W
    W F F F F F F F F F F F F F F F F F F W
    W F F F F F F F F F F F F R F F F F F W
    W F F F F F F F F F F F F F F F F F F W
    W F F F F F F F F F F F F F F F F F F W
    W F F F F F F F F F F F F F F F F F F W
    W F F F F F F F F F F F F F F F F F F W
    W F F F F F F F F F F F F F F F F F F W
    W F F F F F F F F F F F F F F F F F F W
    W F F F F F F F F F F F F F F F F F F W
    W F F F F F F F F F F F F F F F F F F W
    W F F F F F F F F F F F F F F F F F F W
    W F F F F F F F F F F F F S T P F F F W
    W W W W W W W W W W W W W W W W W W W W
    "
}

pub fn level2_map() -> &'static str {
    "
    W W W W W W W W W W W W W W W W W W W W
    W F F F F F F F F F F F F F F F F F F W
    W F F W W W F F F F F F F W W W F F F W
    W F F W F F F F F F F F F F W F F F F W
    W F F W F B F F F F F F R F W F F F F W
    W F F W F F F F F F F F F F W F F F F W
    W F F W F F F F F F F F F F W F F F F W
    W F F W F F F F F F F F F F W F F F F W
    W F F W F F F F F F F F F F W F F F F W
    W F F W F F F F F F F F F F W F F F F W
    W F F W F F F F F F F F F F W F F F F W
    W F F W F F F F F F F F F F W F F F F W
    W F F W F F F F F F F F F F W F F F F W
    W F F W F F F F F F F F F F W F F F F W
    W F F W F F F F F F F F F F W F F F F W
    W F F F F F F F F F F F F F F F F F F W
    W F F F F F F F S T P F F F F F F F F W
    W W W W W W W W W W W W W W W W W W W W
    "
}

pub fn level3_map() -> &'static str {
    "
    W W W W W W W W W W W W W W W W W W W 
    W F F F F F F F F W F F F F F F F F W 
    W F F F F F F F F W F F F F F F F F W 
    W F N N N N N F F W F F W W W W W F W 
    W F N F F F N F F W F F W F F F W F W 
    W F N F B F F F F W F F F F F F W F W 
    W F N F F F N F F W F F W F F F W F W 
    W F N F F F N F F W F F W F F F W F W 
    W F N F F F N F F W F F W F F F W F W 
    W F N F F F N F F W F F W F F F W F W 
    W F N F F F N F F W F F W F F F W F W 
    W F N F F F N F F W F F W F F F W F W 
    W F N F R F F F F W F F F F F F W F W 
    W F N F F F N F F W F F W F F F W F W 
    W F N N N N N F F W F F W W W W W F W 
    W F F F F F F F F W F F F F F F F F W 
    W F F F S T P F F W F F F F F F F F W 
    W W W W W W W W W W W W W W W W W W W 
    "
}

// 修改初始化函数支持关卡参数
pub fn initialize_level(world: &mut World, context: &mut Context, level: usize) {
    let map_str = match level {
        1 => level1_map(),
        2 => level2_map(),
        3 => level3_map(),
        _ => level1_map(), // 默认关卡1
    };

    load_map(world, map_str.to_string());
    load_sounds(world, context);
}

pub fn load_map(world: &mut World, map_string: String) {
    // read all lines
    let rows: Vec<&str> = map_string.trim().split('\n').map(|x| x.trim()).collect();

    for (y, row) in rows.iter().enumerate() {
        let columns: Vec<&str> = row.split(' ').collect();

        for (x, column) in columns.iter().enumerate() {
            // Create the position at which to create something on the map
            let position = Position {
                x: x as u8,
                y: y as u8,
                z: 0, // we will get the z from the factory functions
            };

            // Figure out what object we should create
            match *column {
                "F" => {
                    create_floor(world, position);
                }
                "W" => {
                    create_floor(world, position);
                    create_wall(world, position);
                }
                "P" => {
                    create_floor(world, position);
                    create_player(world, position);
                }
                "B" => {
                    create_floor(world, position);
                    create_box(world, position, BoxColour::Blue);
                }
                "R" => {
                    create_floor(world, position);
                    create_box(world, position, BoxColour::Red);
                }
                "S" => {
                    create_floor(world, position);
                    create_box_spot(world, position, BoxColour::Blue);
                }
                "T" => {
                    create_floor(world, position);
                    create_box_spot(world, position, BoxColour::Red);
                }
                "N" => {
                    create_floor(world, position);      // 渲染为地板
                    create_air_wall(world, position); 
                }
                _ => (), 
            }
        }
    }
}

pub fn load_sounds(world: &mut World, context: &mut Context) {
    let mut query = world.query::<&mut crate::components::AudioStore>();
    let audio_store = query.iter().next().unwrap().1;

    let sounds = ["correct", "incorrect", "wall", "vectory", "failed"];

    for sound in sounds.iter() {
        let sound_name = sound.to_string();
        let sound_path = format!("/sounds/{}.wav", sound_name);
        let sound_source = Source::new(context, sound_path).expect("expected sound loaded");

        audio_store
            .sounds
            .insert(sound_name, Box::new(sound_source));
    }
}

pub fn clear_level(world: &mut World) {
    // Remove all game entities except system entities
    let entities_to_remove: Vec<hecs::Entity> = world
        .query::<(&Position,)>()
        .iter()
        .map(|(entity, _)| entity)
        .collect();
    
    for entity in entities_to_remove {
        let _ = world.despawn(entity);
    }
}

